#include "ToolBox.h"
#include "Editor.h"
#include "State.h"
#include "PointerState.h"
#include "MoveState.h"
#include "BrushState.h"
#include "ClipBrushState.h"
#include "EntityState.h"
#include "../resource.h"

namespace FinalRendering
{
	namespace Editor
	{
		Header::Header()
		{
			Text = new Label();
		}

		void Header::Draw(int absX, int absY)
		{
			absX += Left;
			absY += Top;
			if (!Visible)
				return;
			Graphics::GradiantBrushColor1 = Graphics::GradiantBrushColor2 = GxRendering::UI::Color(251,251,251,255);
			Graphics::GradiantBrushColor3 = Graphics::GradiantBrushColor4 = GxRendering::UI::Color(214,217,218,255);
			Graphics::FillRectangle(absX, absY, absX+Width, absY+Height, true);
			Text->Left = 4;
			Text->Top = (Height-Text->GetHeight())/2;
			Text->Draw(absX, absY);
		}

		ToolBox::ToolBox()
		{
			tools = new ToolStrip();
			AddChild(tools);
			header = new Header();
			AddChild(header);

			header->SetHeight(20);
			header->DockStyle = Control::dsTop;
			header->Text->SetText(L"Tools");
			tools->DockStyle = Control::dsFill;
			tools->AddButton(L"Pointer", &Bitmap(IDB_POINTER),&Color(0,0,0,255))->OnClick.Bind(this, &ToolBox::btnPointerClicked);
			tools->AddButton(L"Brush", &Bitmap(IDB_BRUSH),&Color(0,0,0,0))->OnClick.Bind(this, &ToolBox::btnBrushClicked);
			tools->AddButton(L"Entity", &Bitmap(IDB_ENTITY), &Color(0,0,0,0))->OnClick.Bind(this, &ToolBox::btnEntityClicked);
			tools->AddButton(L"Clip Brush", &Bitmap(IDB_CLIP), &Color(0,0,0,0))->OnClick.Bind(this, &ToolBox::btnClipBrushClicked);
			tools->Orientation = ToolStrip::Vertical;
		}

		TitledPanel::TitledPanel()
		{
			BorderStyle = BS_NONE;
			header = new Header();
			header->SetHeight(24);
			header->DockStyle = Control::dsTop;
			expanded = true;
			maxHeight = 0;
			header->OnClick.Bind(this, &TitledPanel::HeaderClicked);
			AddChild(header);
		}

		void TitledPanel::HeaderClicked(UI_Base * sender)
		{
			if (expanded)
				Collapse();
			else
				Expand();
		}

		void TitledPanel::Expand()
		{
			expanded = true;
			SetHeight(maxHeight);
		}

		void TitledPanel::Collapse()
		{
			expanded = false;
			maxHeight = Height;
			SetHeight(header->GetHeight());
		}

		void TitledPanel::SetText(const String & text)
		{
			header->Text->SetText(text);
		}

		void ToolBox::UncheckAll()
		{
			for (int i=0; i<tools->Count(); i++)
				tools->GetButton(i)->Checked = false;
		}

		void ToolBox::btnPointerClicked(UI_Base * sender)
		{
			UncheckAll();
			((ToolButton *) sender)->Checked = true;
			WorldEditor->SetState(new PointerState());
		}

		void ToolBox::btnBrushClicked(UI_Base * sender)
		{
			UncheckAll();
			((ToolButton *) sender)->Checked = true;
			WorldEditor->SetState(new BrushState());
		}

		void ToolBox::btnClipBrushClicked(UI_Base * sender)
		{
			UncheckAll();
			((ToolButton *) sender)->Checked = true;
			WorldEditor->SetState(new ClipBrushState());
		}

		void ToolBox::btnEntityClicked(UI_Base * sender)
		{
			UncheckAll();
			((ToolButton *) sender)->Checked = true;
			WorldEditor->SetState(new EntityState());
		}
	}
}